package com.wangshidai.t2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器
 */
public class ServerDemo {

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket=new ServerSocket(8888);
            System.out.println("服务器启动成功");
            Socket socket = serverSocket.accept();
            System.out.println("客户端上线");

            //接收客户端的消息
            InputStream inputStream = socket.getInputStream();
            InputStreamReader isr=new InputStreamReader(inputStream);
            BufferedReader br=new BufferedReader(isr);

            String message = br.readLine();//读取消息
            System.out.println("客户端说:"+message);

            //发送消息给客户端
            //发送消息就是流的操作
            OutputStream os = socket.getOutputStream();
            PrintStream ps=new PrintStream(os,true);
            ps.println("弟，我吃饭了，你呢？");  //发送消息

            ps.close();

            br.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

}
